package es.udc.emarketapp.emarket.model.countryFacade;

import javax.sql.DataSource;

import java.util.List;

import es.udc.emarketapp.emarket.model.country.vo.CountryVO;
import es.udc.emarketapp.emarket.model.countryFacade.actions.FindByIdAction;
import es.udc.emarketapp.emarket.model.countryFacade.actions.FindAllCountriesAction;
import es.udc.emarketapp.emarket.model.countryFacade.actions.FindAvaliableCountriesToSpecifyShippingAction;
import es.udc.emarketapp.emarket.model.countryFacade.actions.FindIdByNameAction;
import es.udc.emarketapp.emarket.model.countryFacade.actions.FindShippingCountriesAction;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.PlainActionProcessor;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;

public class CountryFacade {

    private DataSource dataSource;

    public CountryFacade() throws InternalErrorException {
        dataSource = new SimpleDataSource();
    }

    /*
    public CountryVO findById(String idPais)
            throws InstanceNotFoundException, InternalErrorException {
        try {
            FindByIdAction action = new FindByIdAction(idPais);
            return (CountryVO) PlainActionProcessor.process(dataSource, action);
        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    }//findById
     * 
     */

    public CountryVO findById(String idPais, String language)
            throws InstanceNotFoundException, InternalErrorException {
        try {
            FindByIdAction action = new FindByIdAction(idPais, language);
            return (CountryVO) PlainActionProcessor.process(dataSource, action);
        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    }//findById

    public String findIdByName(String nomPais)
            throws InstanceNotFoundException, InternalErrorException {
        try {
            FindIdByNameAction action = new FindIdByNameAction(nomPais);
            return (String) PlainActionProcessor.process(dataSource, action);
        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    }//findIdByName

  /*
    public List<CountryVO> findAllCountries() throws InternalErrorException {

        try {
            FindAllCountriesAction action = new FindAllCountriesAction();

            return (List<CountryVO>) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    }//findAllCountries
   *
   */

    public List<CountryVO> findAllCountries(String language) throws InternalErrorException {

        try {
            FindAllCountriesAction action = new FindAllCountriesAction(language);

            return (List<CountryVO>) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    }//findAllCountries

    public List<CountryVO> findAvaliableCountriesToSpecifyShippingAction(
            String idSeller, String language)
            throws InternalErrorException {

        try {
            FindAvaliableCountriesToSpecifyShippingAction action =
                    new FindAvaliableCountriesToSpecifyShippingAction(idSeller, language);

            return (List<CountryVO>) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    }//findAvaliableCountriesToSpecifyShippingAction

    public List<CountryVO> findShippingCountriesAction(String idSeller, String language)
            throws InternalErrorException {

        try {
            FindShippingCountriesAction action =
                    new FindShippingCountriesAction(idSeller, language);

            return (List<CountryVO>) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    }//findShippingCountriesAction

} // class

